package controller

import (
	"fmt"
	"net/http"
	"strconv"
	"testt/common"
	"testt/model"

	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func FindPactByID(ctx *gin.Context) {
	DB := common.GetDB()
	requestpact := make(map[string]interface{})
	fmt.Println("修该执行")
	var pacts model.Pacts
	DB.Last(&pacts)
	ctx.ShouldBind(&requestpact)
	ctx.JSON(http.StatusOK, requestpact)
}

func Find_allPact(ctx *gin.Context) {
	var pacts []model.Pacts
	db := common.GetDB()
	result := db.Find(&pacts)
	fmt.Println(pacts)
	if result != nil {
		fmt.Println("data")
	}
	ctx.JSON(http.StatusOK, pacts)
}

func AddPact(ctx *gin.Context) {
	DB := common.GetDB()
	requestpact := make(map[string]interface{})
	//获取参数
	var pact model.Pacts
	DB.Last(&pact)
	ctx.ShouldBind(&requestpact)
	fmt.Println(requestpact, "我是添加合同")
//	pact_id := pact.Pact_id + 1
	pact_id, _ := strconv.Atoi(requestpact["Pact_id"].(string))
	pact_date := requestpact["Pact_date"].(string)
	// product_id := requestpact["Product_id"].(string)
	// product_type := requestpact["Product_type"].(string)
	// product_number, _ := strconv.Atoi(requestpact["Product_number"].(string))
	// product_price, _ := strconv.Atoi(requestpact["Product_price"].(string))
	// supplier := requestpact["Supplier"].(string)
	// deliver_date := requestpact["Deliver_date"].(string)
	// supply_place := requestpact["Supply_place"].(string)
	// supply_phone := requestpact["Supply_phone"].(string)
	// supply_way := requestpact["Supply_way"].(string)
	// supply_name := requestpact["Supply_name"].(string)
	//判断该合同是否存在
	if isExists(DB, pact_id) {
		ctx.JSON(200, gin.H{
			"code": 200,
			"msg":  "合同已经存在",
		})
		return
	}
	newPact := model.Pacts{
		Pact_id:        pact_id,
		Pact_date:      pact_date,
		// Product_id:     product_id,
		// Product_type:   product_type,
		// Product_number: product_number,
		// Product_price:  product_price,
		// Supplier:       supplier,
		// Deliver_date:   deliver_date,
		// Supply_place:   supply_place,
		// Supply_phone:   supply_phone,
		// Supply_way:     supply_way,
		// Supply_name:    supply_name,
	}
	DB.Create(&newPact)
	//返回结果
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
}

func isExists(db *gorm.DB, id int) bool { //string
	var pact model.Pacts
	db.Where("pact_id = ?", id).First(&pact)
	if pact.Pact_id != 0 {
		return true
	} else {
		return false
	}
}

func DeletePact(ctx *gin.Context) {
	DB := common.GetDB()
	fmt.Println("我执行了")
	requestpact := make(map[string]interface{})
	var pacts model.Pacts
	//获取参数
	ctx.ShouldBind(&requestpact)
	fmt.Println(requestpact["Pact_id"].(float64), "合同编号")
	pacts.Pact_id = int(requestpact["Pact_id"].(float64))
	DB.Where("pact_id = ?", pacts.Pact_id).Delete(&pacts)
}

func UpdatePact(ctx *gin.Context) {
	DB := common.GetDB()
	fmt.Println("我是修该合同信息")
	requestpact := make(map[string]interface{})
	//获取参数
	var pacts model.Pacts
	DB.Last(&pacts)
	ctx.ShouldBind(&requestpact)
	pacts.Pact_id = int(requestpact["Pact_id"].(float64))
	fmt.Println("1")
	pacts.Pact_date = requestpact["Pact_date"].(string)
	// pacts.Product_id = requestpact["Product_id"].(string)
	// pacts.Product_type = requestpact["Product_type"].(string)
	// pacts.Product_number = int(requestpact["Product_number"].(float64))
	// fmt.Println("2")
	// pacts.Product_price = int(requestpact["Product_amount"].(float64))
	// pacts.Supplier = requestpact["Supplier"].(string)
	// pacts.Deliver_date = requestpact["Deliver_date"].(string)
	// pacts.Supply_place = requestpact["Supply_place"].(string)
	// pacts.Supply_phone = requestpact["Supply_phone"].(string)
	// pacts.Supply_way = requestpact["Supply_way"].(string)
	// pacts.Supply_name = requestpact["Supply_name"].(string)
	fmt.Println("2")
	fmt.Println(pacts)
	DB.Where("pact_id = ?", pacts.Pact_id).Updates(&pacts)
	ctx.JSON(200, gin.H{
		"code": 200,
		"msg":  "修改成功",
	})
}
